package com.foreverwin.jzymes.qc.check.service.impl;


import com.alibaba.fastjson.JSONObject;
import com.foreverwin.jzymes.common.constant.Constants;
import com.foreverwin.jzymes.common.domain.*;
import com.foreverwin.jzymes.common.enums.TaskStatusEnum;
import com.foreverwin.jzymes.common.enums.TaskTypeEnum;
import com.foreverwin.jzymes.common.mapper.TranshipInfraAdviceMapper;
import com.foreverwin.jzymes.common.service.*;
import com.foreverwin.jzymes.common.util.BOHandleUtil;
import com.foreverwin.jzymes.common.util.CollectionUtil;
import com.foreverwin.jzymes.common.util.CommonMethods;
import com.foreverwin.jzymes.framework.util.DateUtil;
import com.foreverwin.jzymes.framework.util.I18nUtil;
import com.foreverwin.jzymes.job.TaskService;
import com.foreverwin.jzymes.production.nc.service.ICarrierNCRecordAdviceService;
import com.foreverwin.jzymes.qc.check.domain.QcCheck;
import com.foreverwin.jzymes.qc.check.domain.QcCheckFai;
import com.foreverwin.jzymes.qc.check.domain.QcCheckFaiFail;
import com.foreverwin.jzymes.qc.check.mapper.FAIAdviceMapper;
import com.foreverwin.jzymes.qc.check.mapper.QcCheckAdiviceMapper;
import com.foreverwin.jzymes.qc.check.service.*;
import org.apache.activemq.command.ActiveMQTopic;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import java.math.BigDecimal;
import java.util.*;

@Service
public class QcCheckAdiviceServiceImpl implements IQcCheckAdiviceService {

    Logger logger = LoggerFactory.getLogger( QcCheckAdiviceServiceImpl.class );

    @Autowired
    IResourceAdviceService resourceAdviceService;
    @Autowired
    ISiteAdviceService siteAdviceService;
    @Autowired
    IQcCheckService  qcCheckService;
    @Autowired
    IQcCheckNcService qcCheckNcService;
    @Autowired
    IQcCheckFaiService qcCheckFaiService;
    @Autowired
    IItemGroupAdviceService  itemGroupAdviceService;
    @Autowired
    ISysTaskService sysTaskService;
    @Autowired
    ICarrierNCRecordAdviceService carrierNCRecordAdviceService;
    @Autowired
    TaskService taskService;
    @Autowired
    ICncReqLogAdviceService cncReqLogAdviceService;
    @Autowired
    ISystemRuleAdviceService systemRuleAdviceService;
    @Autowired
    INcDataService ncDataService;
    @Autowired
    INCDataAdviceService ncDataAdviceService;
    @Autowired
    IDCGroupAdviceService dcGroupAdviceService;
    @Autowired
    ICarrierBindAdviceService carrierBindAdviceService;
    @Autowired
    IQcCheckFaiFailService qcCheckFaiFailService;
    @Autowired
    IItemAdviceService itemAdviceService;
    @Autowired
    ICustomFieldsAdviceService customFieldsAdviceService;
    @Autowired
    INCCodeAdviceService ncCodeAdviceService;
    @Autowired
    IProductionShiftService productionShiftService;
    @Autowired
    ITranshipInfraService transhipInfraService;
    @Autowired
    ICncReqLogService cncReqLogService;
    @Autowired
    TranshipInfraAdviceMapper transhipInfraAdviceMapper;
    @Autowired
    ICommonService commonService;
    @Autowired
    FAIAdviceMapper faiAdviceMapper;
    @Autowired
    QcCheckAdiviceMapper qcCheckAdiviceMapper;
    @Autowired
    JmsMessagingTemplate jmsMessagingTemplate;

    private String OPERATION_INSPECT;

    /**
     * 提交三次元/目检检测结果
     *
     * @param taskType
     * @param qcCheck
     * @param ncList
     * @param faiId
     */
    @Transactional( propagation = Propagation.REQUIRED , rollbackFor = Exception.class)
    public void submit( String taskType, QcCheck qcCheck, List<NcData> ncList, String faiId ) {

        /**加载系统规则*/
        preload( qcCheck.getSite() );

        /**检查jsonMap字段*/
        checkFields( qcCheck );

        if( TaskTypeEnum.FAI == TaskTypeEnum.valueOf( taskType ) ){
            Assert.hasText( faiId, I18nUtil.getI18nText( "qc.check.qcFAINotBlank" ) );
        }

        /**保存检验主数据*/
        saveQcCheck( taskType, qcCheck, "M" );

        /**保存不良数据明细*/
        saveQcCheckNc( qcCheck, ncList );

        /**保存FAI测量数据明细*/
        saveQcCheckFai( taskType, qcCheck, faiId );

        /**关闭任务*/
        closeSysTask( qcCheck );

        /**检查是否自动线设备*/
        boolean isAutoLineResource = resourceAdviceService.isAutoLineResource( qcCheck.getSite(), qcCheck.getResrce() );

        /**不良追溯*/
        if( "Y".equals( qcCheck.getIsTrace() ) && isAutoLineResource ){
            traceCarrier( qcCheck.getSite(), qcCheck.getCarrier(), qcCheck.getResrce(), ncList );
        }
        /**发送停机指令*/
        if( "Y".equals( qcCheck.getIsStop() ) && isAutoLineResource ){
            sendStopCommand( qcCheck.getSite(), qcCheck.getResrce() );
        }
    }

    /**
     * 处理三次元测量数据
     *
     * @param faiId
     */
    @Transactional( propagation = Propagation.REQUIRED, rollbackFor = Exception.class )
    public void processfaiData( String faiId ){
        logger.info( "接收到FAI测试数据：" + faiId );
        Map<String,Object> faiData = faiAdviceMapper.selectFAIDataById( faiId );
        if( faiData != null ){
            String site = (String) faiData.get( "SITE" );
            String resource = ((String) faiData.get( "SN" )).toUpperCase();
            String carrier = ((String) faiData.get( "CARRIER" )).toUpperCase();
            String faiResource = (String) faiData.get( "EAP_ID" );

            QcCheck qcCheck = new QcCheck();
            qcCheck.setSite( site );
            qcCheck.setResrce( resource );
            qcCheck.setCarrier( carrier );
            qcCheck.setFaiResrce( faiResource );

            /**解析检验结果*/
            List<QcCheckFaiFail> qcCheckFaiFailList = resolveFaiData( qcCheck, faiData );
            if( qcCheckFaiFailList.size() == 0 ){
                qcCheck.setCheckResult( "Y" );
            }else {
                qcCheck.setCheckResult( "N" );
            }

            if( "Y".equals( qcCheck.getCheckResult() ) ){
                /**保存检验主数据*/
                saveQcCheck( TaskTypeEnum.FAI.name(), qcCheck, "A" );

                /**保存FAI测量数据明细*/
                saveQcCheckFai( TaskTypeEnum.FAI.name(), qcCheck, faiId );

                /**检验数据合格自动关闭任务*/
                closeSysTask( qcCheck );

                /**本体过站*/
                //commonService.sfcPassByCarrier( site, carrier, resource, qcCheck.getUpdatedDateTime() );
            }
            logger.info( "接收到FAI测试数据：" + faiId + ",解析检验结果：" + qcCheck.getCheckResult() + ",处理完成" );
        }else{
            logger.info( "接收到FAI测试数据：" + faiId + ",对应数据不存在" );
        }
    }

    /**
     * 处理cnc上传抽检本体信息
     *
     * @param site
     * @param carrier
     * @param resource
     * @param taskId
     * @param sendTime
     */
    @Transactional( propagation = Propagation.REQUIRED, rollbackFor = Exception.class )
    public void retrieveQcCheckCarrier( String site, String carrier, String resource, String taskId, Date sendTime ){
        /*QcCheck qcCheck = findQcCheckTask( site, taskId );
        if( qcCheck != null ){
            qcCheck.setCarrier( carrier );
            qcCheckService.updateQcCheck( qcCheck );
        }*/

        /**调整一夹待过站数据*/
        List<CarrierBind> carrierBindList = carrierBindAdviceService.existCarrierBindonBind( site, carrier );
        for( CarrierBind carrierBind : carrierBindList ){
            commonService.reduceOpenSfcBatchPassLogQty( carrierBind.getSite(), carrierBind.getItemBo(), resource, 1L );
        }

        /**调整接驳站本体数量*/
        String tranship = resourceAdviceService.findTranship( site, resource );
        String shift = productionShiftService.findShift( site, new Date() );
        Date[] dateRange = productionShiftService.findShiftRange( site, shift );
        TranshipInfra transhipInfra = transhipInfraAdviceMapper.selectTranshipInfraByDate( site, tranship, shift, dateRange[0], dateRange[1] );
        Long carrierQty = 1L;
        if( transhipInfra == null ){
            transhipInfra = new TranshipInfra();
            transhipInfra.setHandle( BOHandleUtil.buildTranshipInfraHandle() );
            transhipInfra.setSite( site );
            transhipInfra.setShift( shift );
            transhipInfra.setResrce( tranship );
            transhipInfra.setCreatedDateTime( new Date() );
            transhipInfra.setProductQty( -1 * carrierQty );
            transhipInfraService.insertTranshipInfra( transhipInfra );
        }else{
            transhipInfra.setProductQty( transhipInfra.getProductQty() - carrierQty );
            transhipInfraService.updateTranshipInfra( transhipInfra );
        }

        /**保存上传信息*/
        CncReqLog cncReqLog = new CncReqLog();
        cncReqLog.setSite( site );
        cncReqLog.setCarrier( carrier );
        cncReqLog.setResrce( resource );
        cncReqLog.setSendTime( sendTime );
        cncReqLog.setCreatedDateTime( new Date() );
        cncReqLog.setHandle( BOHandleUtil.buildCncReqLogHandle() );
        cncReqLogService.insertCncReqLog( cncReqLog );
    }

    /*public void saveFaiNcData( QcCheck qcCheck ){
        List<CarrierBind> carrierBindList = carrierBindAdviceService.findCarrierBindOnBind( qcCheck.getSite(), qcCheck.getCarrier() );
        for( CarrierBind carrierBind : carrierBindList ){
            NcData ncData = new NcData();
            ncData.setSite( carrierBind.getSite() );
            ncData.setCarrier( carrierBind.getCarrier() );
            ncData.setItemBo( carrierBind.getItemBo() );
            ncData.setMachineType( carrierBind.getMachineType() );
            ncData.setResrce( qcCheck.getResrce() );
            ncData.setOperation( resourceAdviceService.findOperationByResource( carrierBind.getSite(), qcCheck.getResrce() ) );
            ncData.setNcCode( "FAI_FAIL" );
            ncData.setNcQty( 1L );
            ncData.setCreatedDateTime( qcCheck.getCreatedDateTime() );
            ncData.setCreateUserBo( qcCheck.getCreateUserBo() );
            commonService.saveNcData( ncData, true );

            QcCheckNc qcCheckNc = new QcCheckNc();
            qcCheckNc.setHandle( BOHandleUtil.buildQcCheckNcHandle() );
            qcCheckNc.setQcCheckBo( qcCheck.getHandle() );
            qcCheckNc.setNcDataBo( ncData.getHandle() );
            qcCheckNcService.insertQcCheckNc( qcCheckNc );
        }
    }*/

    public List<QcCheckFaiFail> resolveFaiData( QcCheck qcCheck, Map<String,Object> faiData ){
        String itemGroup = (String) faiData.get( "DEVICE" );
        String cncType = (String) faiData.get( "DEVICE_TYPE" );
        String dcGroup = itemGroup + "_" + cncType;
        List<Map<String,Object>> dcParameterList = dcGroupAdviceService.findDCParameterList( qcCheck.getSite(), dcGroup );
        List<QcCheckFaiFail> qcCheckFaiFailList = new ArrayList<>();

        for( Map<String,Object> dcParameter : dcParameterList ){
            String parameterName = (String) dcParameter.get( "PARAMETER_NAME" );
            String dbFieldName = (String) dcParameter.get( "PROMPT" );
            BigDecimal minValueGap = (BigDecimal) dcParameter.get( "MIN_VALUE" );
            BigDecimal maxValueGap = (BigDecimal) dcParameter.get( "MAX_VALUE" );
            BigDecimal dcValueMask = new BigDecimal((String) dcParameter.get( "DC_VALUE_MASK" ));
            BigDecimal minValue = dcValueMask.add( minValueGap );
            BigDecimal maxValue = dcValueMask.add( maxValueGap );
            Date testTime = (Date) dcParameter.get( "TEST_TIME" );

            BigDecimal measureValue = (BigDecimal) faiData.get( dbFieldName );
            BigDecimal outTol = BigDecimal.ZERO;

            if( measureValue.compareTo( minValue ) < 0 ){
                outTol = measureValue.subtract( minValue );
            }else if ( measureValue.compareTo( maxValue ) > 0 ){
                outTol = measureValue.subtract( maxValue );
            }

            if( outTol.compareTo( BigDecimal.ZERO ) != 0 ){
                QcCheckFaiFail qcCheckFaiFail = new QcCheckFaiFail();
                qcCheckFaiFail.setHandle( BOHandleUtil.buildQcCheckFaiFailHandle() );
                qcCheckFaiFail.setSite( qcCheck.getSite() );
                qcCheckFaiFail.setTaskId( qcCheck.getTaskId() );
                qcCheckFaiFail.setCarrier( qcCheck.getCarrier() );
                qcCheckFaiFail.setResrce( qcCheck.getResrce() );
                qcCheckFaiFail.setFaiResrce( qcCheck.getFaiResrce() );
                qcCheckFaiFail.setParameterName( parameterName );
                qcCheckFaiFail.setNominal( dcValueMask );
                qcCheckFaiFail.setUpperTol( maxValue );
                qcCheckFaiFail.setLowerTol( minValue );
                qcCheckFaiFail.setMeasure( measureValue );
                qcCheckFaiFail.setOutTol( outTol );
                qcCheckFaiFail.setTestTime( testTime );
                qcCheckFaiFail.setCreatedDateTime( qcCheck.getCreatedDateTime() );
                qcCheckFaiFail.setCreateUserBo( qcCheck.getCreateUserBo() );
                qcCheckFaiFailList.add( qcCheckFaiFail );
            }

        }

        return qcCheckFaiFailList;

    }

    /**
     * 保存检验主数据
     *
     * @param taskType
     * @param qcCheck
     */
    public void saveQcCheck( String taskType, QcCheck qcCheck, String createType ){
        String taskId = qcCheck.getTaskId();
        if( StringUtils.isBlank( taskId ) ){
            taskId = taskService.createTask( qcCheck.getSite(), TaskTypeEnum.valueOf( taskType ), qcCheck.getResrce(), createType );
        }
        String qcCheckBo = BOHandleUtil.buildQcCheckHandle( qcCheck.getSite(), taskId );
        qcCheck.setHandle( qcCheckBo );
        qcCheck.setTaskId( taskId );
        qcCheck.setUpdatedDateTime( new Date() );
        qcCheck.setUpdateUserBo( CommonMethods.getUserBo() );
        qcCheckService.updateQcCheck( qcCheck );
    }

    /**
     * 保存不良数据明细
     *
     * @param qcCheck
     * @param ncList
     */
    public void saveQcCheckNc( QcCheck qcCheck, List<NcData> ncList ){
        String operation = resourceAdviceService.findOperationByResource( qcCheck.getSite(), qcCheck.getResrce() );
        for( NcData ncData : ncList ){
            ncData.setSite( qcCheck.getSite() );
            ncData.setCarrier( qcCheck.getCarrier() );
            ncData.setResrce( qcCheck.getResrce() );
            ncData.setCnc( qcCheck.getResrce() );
            ncData.setOperation( operation );
        }
        carrierNCRecordAdviceService.recordCarrierNC( qcCheck.getSite(), qcCheck.getCarrier(), qcCheck.getResrce(), ncList, false );
    }

    /**
     * 保存FAI测量数据明细
     *
     * @param taskType
     * @param qcCheck
     * @param faiId
     */
    public void saveQcCheckFai( String taskType, QcCheck qcCheck, String faiId ){
        if( TaskTypeEnum.FAI == TaskTypeEnum.valueOf( taskType ) ){
            Map<String,Object> faiData = faiAdviceMapper.selectFAIDataById( faiId );
            faiAdviceMapper.insertCNCParamValue( faiData );

            QcCheckFai qcCheckFai = new QcCheckFai();
            qcCheckFai.setHandle( BOHandleUtil.buildQcCheckFaiHandle() );
            qcCheckFai.setQcCheckBo( qcCheck.getHandle() );
            qcCheckFai.setQcFaiBo( faiId );
            qcCheckFaiService.insertQcCheckFai( qcCheckFai );
        }
    }

    /**
     * 关闭任务
     *
     * @param qcCheck
     */
    public void closeSysTask( QcCheck qcCheck ){
        /**修改任务状态*/
        String taskHandle = BOHandleUtil.buildSysTaskHandle( qcCheck.getSite(), qcCheck.getTaskId() );
        SysTask task = new SysTask();
        task.setHandle( taskHandle );
        task.setTaskStatus( TaskStatusEnum.CLOSED.value() );
        task.setUpdatedDateTime( qcCheck.getUpdatedDateTime() );
        task.setUpdateUserBo( qcCheck.getUpdateUserBo() );
        sysTaskService.updateSysTask( task );
    }

    /**
     * 不良追溯
     *
     * @param site
     * @param carrier
     * @param resource
     * @param ncList
     */
    public void traceCarrier( String site, String carrier, String resource, List<NcData> ncList ){

        List<String> carrierList =  cncReqLogAdviceService.findPrevFiveCarrier( site, carrier, resource );
        for( String _carrier : carrierList ){
            for( NcData ncData : ncList ){
                if( StringUtils.isBlank( ncData.getNcCode() ) ){
                    continue;
                }
                NcData ncData1 = ncDataAdviceService.findItemOpenNcDataFromDate( ncData.getSite(), _carrier, ncData.getItemBo(), OPERATION_INSPECT );
                if( ncData1 == null ){
                    NcData _ncData = new NcData();
                    _ncData.setSite( ncData.getSite() );
                    _ncData.setMachineType( ncData.getMachineType() );
                    _ncData.setCarrier( _carrier );
                    _ncData.setResrce( resource );
                    _ncData.setOperation( OPERATION_INSPECT );
                    _ncData.setItemBo( ncData.getItemBo() );
                    _ncData.setCnc( ncData.getCnc() );
                    _ncData.setNcCode( ncData.getNcCode() );
                    _ncData.setNcQty( ncData.getNcQty() );
                    _ncData.setCreatedDateTime( ncData.getCreatedDateTime() );
                    _ncData.setCreateUserBo( ncData.getCreateUserBo() );
                    _ncData.setHandle( BOHandleUtil.buildNCDataHandle() );
                    ncDataService.insertNcData( _ncData );
                }
            }
        }
    }

    public void sendStopCommand( String site, String resrce ){
        JSONObject json = new JSONObject();
        json.put( "MESSAGE_TYPE" , "MES_REMOTE_COMMAND" );
        json.put( "SITE" , site );
        json.put( "RESRCE" , resrce );
        json.put( "COMMAND" , "STOP" );
        json.put( "SEND_TIME" , DateUtil.format( new Date() ) );
        ActiveMQTopic topic = new ActiveMQTopic("MES_REMOTE_COMMAND" );
        jmsMessagingTemplate.convertAndSend( topic, json.toString() );
    }

    /**
     * 加载系统规则
     *
     * @param site
     */
    public void preload( String site ){

        /**获取系统规则全检工序*/
        OPERATION_INSPECT = systemRuleAdviceService.existSystemRule( site, Constants.SYSTEM_RULE_OPERATION_INSPECT );
    }

    /**检查jsonMap字段*/
    public  void  checkFields( QcCheck qcCheck ){

        /**检查站点*/
        siteAdviceService.existSite( qcCheck.getSite() );
        /**获取机种*/
        String itemGroup =  itemGroupAdviceService.findItemGroupBySite( qcCheck.getSite() );
        qcCheck.setMachineType( itemGroup );
        /**检查本体编号*/
        Assert.hasText( qcCheck.getCarrier(), I18nUtil.getI18nText( "common.carrierNotBlank" ));
        /**检查设备*/
        resourceAdviceService.existResource( qcCheck.getSite(), qcCheck.getResrce() );
        /**检测结果为Y/N*/
        Assert.hasText( qcCheck.getCheckResult(), I18nUtil.getI18nText( "qc.check.checkResult" ));
        /**停机Y/N*/
        if( "true".equals( qcCheck.getIsStop() ) ){
            qcCheck.setIsStop( "Y" );
        }else{
            qcCheck.setIsStop( "N" );
        }
        /**拦截追溯Y/N*/
        if( "true".equals( qcCheck.getIsTrace() ) ){
            qcCheck.setIsTrace( "Y" );
        }else{
            qcCheck.setIsTrace( "N" );
        }
    }

    /**
     * 构建FAI不合格明细
     *
     * @param site
     * @param carrier
     * @return
     */
    public List<Map<String,Object>> buildFaiNcDataList( String site, String carrier ){
        List<CarrierBind> carrierBindList = carrierBindAdviceService.findCarrierBindOnBind( site, carrier );
        List<Map<String,Object>> rtnNcDataList = new ArrayList<>();
        for( CarrierBind carrierBind : carrierBindList ){
            Map<String,Object> rtnNcDataMap = new HashMap<>();
            Map<String,Object> itemMap = itemAdviceService.existItem( carrierBind.getItemBo() );
            rtnNcDataMap.put( "itemBo", itemMap.get( "HANDLE" ) );
            rtnNcDataMap.put( "item", itemMap.get( "ITEM" ) );
            rtnNcDataMap.put( "revision", itemMap.get( "REVISION" ) );
            rtnNcDataMap.put( "description", customFieldsAdviceService.existCustomValue((String) itemMap.get( "HANDLE" ), Constants.ITEM_CUSTOM_FIELD_ITEM_LABEL ) );
            Map<String,Object> ncCodeMap = ncCodeAdviceService.existNCCode( site, "FAI_FAIL" );
            rtnNcDataMap.put( "ncCode", ncCodeMap.get( "NC_CODE" ) );
            rtnNcDataMap.put( "ncDesc", ncCodeMap.get( "DESCRIPTION" ) );
            rtnNcDataList.add( rtnNcDataMap );
        }
        CollectionUtil.sort( rtnNcDataList, "description" );
        return rtnNcDataList;
    }

    /**
     * 查询打开状态qc任务
     *
     * @param site
     * @param taskTypeEnum
     * @param resource
     * @return
     */
    public List<QcCheck> findOpenQcCheck( String site, TaskTypeEnum taskTypeEnum, String resource ){
        Assert.hasText( site, I18nUtil.getI18nText( "common.siteNotBlank" ) );
        Assert.notNull( taskTypeEnum, I18nUtil.getI18nText( "common.taskTypeNotBlank" ) );
        Assert.hasText( resource, I18nUtil.getI18nText( "common.resourceNotBlank" ) );
        String itemGroup = itemGroupAdviceService.findItemGroupBySite( site );
        Assert.hasText( itemGroup, I18nUtil.getI18nText( "common.itemGroupNotBlank" ) );

        List<QcCheck> qcCheckList = qcCheckAdiviceMapper.selectQcCheckTask( site, itemGroup, taskTypeEnum.name(), TaskStatusEnum.OPEN.value(), resource );

        return qcCheckList;
    }

    /**
     * 查询打开状态qc任务
     *
     * @param site
     * @param taskTypeEnum
     * @param carrier
     * @param resource
     * @return
     */
    public List<QcCheck> findOpenQcCheck( String site, TaskTypeEnum taskTypeEnum, String carrier, String resource ){
        Assert.hasText( site, I18nUtil.getI18nText( "common.siteNotBlank" ) );
        Assert.notNull( taskTypeEnum, I18nUtil.getI18nText( "common.taskTypeNotBlank" ) );
        Assert.hasText( carrier, I18nUtil.getI18nText( "common.carrierNotBlank" ) );
        Assert.hasText( resource, I18nUtil.getI18nText( "common.resourceNotBlank" ) );

        String itemGroup = itemGroupAdviceService.findItemGroupBySite( site );
        Assert.hasText( itemGroup, I18nUtil.getI18nText( "common.itemGroupNotBlank" ) );

        List<QcCheck> qcCheckList = null;
        qcCheckList = qcCheckAdiviceMapper.selectQcCheckTaskByCarrier( site, itemGroup, taskTypeEnum.name(), TaskStatusEnum.OPEN.value(), carrier, resource );
        if( qcCheckList == null || qcCheckList.size() == 0 ){
            qcCheckList = qcCheckAdiviceMapper.selectQcCheckTask( site, itemGroup, taskTypeEnum.name(), TaskStatusEnum.OPEN.value(), resource );
        }
        return qcCheckList;
    }


    /**
     * 查询关闭状态qc任务
     *
     * @param site
     * @param taskTypeEnum
     * @param resource
     * @return
     */
    public List<QcCheck> findCloseQcCheck( String site, TaskTypeEnum taskTypeEnum, String resource ){
        Assert.hasText( site, I18nUtil.getI18nText( "common.siteNotBlank" ) );
        Assert.notNull( taskTypeEnum, I18nUtil.getI18nText( "common.taskTypeNotBlank" ) );
        Assert.hasText( resource, I18nUtil.getI18nText( "common.resourceNotBlank" ) );
        String itemGroup = itemGroupAdviceService.findItemGroupBySite( site );
        Assert.hasText( itemGroup, I18nUtil.getI18nText( "common.itemGroupNotBlank" ) );

        List<QcCheck> qcCheckList = qcCheckAdiviceMapper.selectQcCheckTask( site, itemGroup, taskTypeEnum.name(), TaskStatusEnum.CLOSED.value(), resource );

        return qcCheckList;
    }

    /**
     * 查询qc任务
     *
     * @param site
     * @param taskId
     * @return
     */
    public QcCheck findQcCheckTask( String site, String taskId ){
        Assert.hasText( site, I18nUtil.getI18nText( "common.siteNotBlank" ) );
        Assert.hasText( taskId, I18nUtil.getI18nText( "common.taskIdNotBlank" ) );

        String taskHandle = BOHandleUtil.buildSysTaskHandle( site, taskId );
        QcCheck qcCheck = qcCheckService.selectQcCheckById( taskHandle );
        return qcCheck;
    }

    /**
     * 查询本体FAI测量数据
     *
     * @param site
     * @param carrier
     * @return
     */
    public Map<String,Object> findFaiDataByCarrier( String site, String carrier ){
        List<CarrierBind> carrierBindList = carrierBindAdviceService.findCarrierBindOnBind( site, carrier );
        if( carrierBindList == null || carrierBindList.size() == 0 ){
            return null;
        }
        Date fromDate = carrierBindList.get( 0 ).getStartDateTime();
        List<Map<String,Object>> faiDataList = faiAdviceMapper.selectFAIDataByCarrier( site, carrier, fromDate );
        if( faiDataList != null && faiDataList.size() > 0 ){
            return faiDataList.get( 0 );
        }
        return null;
    }
}
